package com.ldk.base.framework.util.baidu;

import java.net.URLEncoder;

import com.alibaba.fastjson.JSON;
import com.ldk.base.framework.util.HttpUtil;
import com.ldk.base.framework.util.baidu.routematrix.RouteMatrixRequestParms;
import com.ldk.base.framework.util.baidu.routematrix.RouteMatrixResponseParmsResult;
import com.ldk.base.framework.util.baidu.routematrix.RouteMatrixResponseParms;

/**
 * 批量行驶距离计算
 * @author xiangwl
 * 2017年2月27日
 * com.ldk.base.framework.util.baidu
 * RouteMatrixApi
 */
public class RouteMatrixApi {
	/**
	 * 驾车请求地址
	 */
	private final static String URL_DRIVING = "http://api.map.baidu.com/routematrix/v2/driving?";
	
	/**
	 * 驾车请求
	 * @param request
	 * @return
	 */
	public static RouteMatrixResponseParmsResult driving(RouteMatrixRequestParms request){
		try{
			
			String queryParam = String.format("output=%s&origins=%s&destinations=%s&tactics=%s&ak=%s", 
					request.getOutput(), 
					URLEncoder.encode(request.getOrigins(),"utf-8"), 
					URLEncoder.encode(request.getDestinations(),"utf-8"),
					request.getTactics(),
					request.getAk());
			String response = HttpUtil.get(URL_DRIVING, queryParam, null);
			
			RouteMatrixResponseParms responseParms =  JSON.parseObject(response, RouteMatrixResponseParms.class);
			if(responseParms == null || !responseParms.getStatus().equals(0) || responseParms.getResult().isEmpty())
				return null;
			return responseParms.getResult().get(0);
		}
		catch(Exception e){
			
		}
		return null;
	}
}
